<template>
  <div class="dashboard-container" id="dashboard-container">
    <pageHeader></pageHeader>
    <div class="router-view-container">
      <router-view
        v-if="showRouterView"
        class="animate__faster animate__animated animate__fadeIn"
      ></router-view>
    </div>
  </div>
</template>

<script>
import pageHeader from "@/components/pageHeader/index.vue";
import { mapGetters } from "vuex";
import { flatTreeData } from "@/utils";
import { routes } from "@/router";
import { isEmptyObject } from "@/tools";
import whiteList from "../routerInterceptor/whiteList";
export default {
  name: "Dashboard",
  components: {
    pageHeader,
  },
  computed: {
    ...mapGetters(["currentMenu", "loginData"]),
  },
  watch: {
    currentMenu: {
      handler(val) {
        let { menuCode, path, isFrame, activeRule, openType } = val;
        let routerPath = "";
        if (!menuCode) {
          return;
        }
        if (menuCode.endsWith("HOME_PAGE") || menuCode == "PAGE_TODO") {
          //首页、代办
          routerPath = path;
        } else if (isFrame == 1) {
          //第三方外链
          if (!path || path == "/") {
            routerPath = `/NotFound`;
          } else {
            routerPath = `/iframe?openType=${openType}&url=${encodeURIComponent(
              path
            )}`;
          }
        } else if (path == "/developing") {
          routerPath = "/developing";
        } else {
          let flatData = flatTreeData(routes, "children");
          let hasRoute = flatData.find((v) => v.path == activeRule + path);
          if (hasRoute) {
            routerPath = activeRule + path;
          } else {
            routerPath = "/NotFound";
          }
        }
        //相同路由判断 【强制刷新 router-view】
        if (this.$route.path == routerPath && menuCode.endsWith("HOME_PAGE")) {
          this.showRouterView = false;
          setTimeout((_) => {
            this.showRouterView = true;
          }, 50);
        }
        if (isEmptyObject(this.loginData)) {
          return;
        }
        if (whiteList.includes(this.$route.path)) {
          routerPath = this.$route.path;
        }
        this.$router
          .push({
            path: routerPath,
          })
          .catch((_) => {});
      },
      immediate: true,
      deep: true,
    },
  },
  data() {
    return {
      showRouterView: true,
    };
  },
  mounted() {},
  methods: {},
  destroyed() {},
};
</script>

<style lang="less" scoped>
.dashboard-container {
  width: 100%;
  height: 100%;
  background: #f3f4f6;
  overflow: hidden;
  .flex();
  flex-direction: column;
  .router-view-container {
    flex: 1;
    overflow: hidden;
  }
}
</style>
